package com.christian.singleton;

/**
 * 文件描述：单例模式，双重检测锁：为了解决懒汉式的线程安全问题而产生的。
 *
 * @Author：wang
 * @Date：2020-02-27 09:42:22
 */
public class DoubleDetectionSingleton {

    /**
     * 声明要实例化的对象
     */
    private static volatile DoubleDetectionSingleton INSTANCE;

    /**
     * 构造方法私有化
     */
    private DoubleDetectionSingleton() {

    }

    /**
     * 获取实例对象的方法
     *
     * @return
     */
    public static DoubleDetectionSingleton getInstance() {
        if (INSTANCE == null) {
            //双重检测锁
            synchronized (DoubleDetectionSingleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DoubleDetectionSingleton();
                }
            }
        }

        return INSTANCE;
    }

    public static void main(String[] args) {
        DoubleDetectionSingleton instance1 = getInstance();
        DoubleDetectionSingleton instance2 = getInstance();

        System.out.println("两个对象实例是否相同：" + (instance1 == instance2));
    }
}
